package cn.com.infohold.smartcity.activity.Chengbaofang;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.bean.RelativeBean;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.DialogUtils;
import cn.com.infohold.smartcity.view.WheelView;
import common.annotation.ViewInject;

/**
 * Created by Monsoon on 2017/11/8.
 */
public class RelativeAdd_Activity extends ParentActivity{

    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.et_name)
    EditText et_name;
    @ViewInject(R.id.tv_sex)
    TextView tv_sex;
    @ViewInject(R.id.tv_relationtype)
    TextView tv_relationtype;
    @ViewInject(R.id.et_certificatecode)
    EditText et_certificatecode;
    @ViewInject(R.id.tv_education)
    TextView tv_education;
    @ViewInject(R.id.et_specialty)
    EditText et_specialty;
    @ViewInject(R.id.et_remark)
    EditText et_remark;

    @ViewInject(R.id.btn_commit)
    Button btn_commit;

    private RelativeBean relativeBean;
    String[] sexStr = new String[]{"男","女"};
    String[] relationtypeStr = new String[]{"配偶","父母","子女","兄弟姐妹"};
    String[] educationStr = new String[]{"博士研究生","硕士研究生","大学本科","大学专科","中专/高中","初中","小学","未知"};

    private int resultCode = 101;

    @Override
    protected Object inflateView() {
        return R.layout.acitivity_add_relative;
    }

    @Override
    protected void initView(View anchor) {

        relativeBean = new RelativeBean();

        List<String> sexList = new ArrayList<>();
        Collections.addAll(sexList,sexStr);
        List<String> relationTypeList = new ArrayList<>();
        Collections.addAll(relationTypeList,relationtypeStr);
        List<String> educationList = new ArrayList<>();
        Collections.addAll(educationList,educationStr);

        ib_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransitionFade();
            }
        });

        tv_sex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.createNewSelectedDialog(RelativeAdd_Activity.this, "性别", new WheelView.OnWheelViewOkClickListener() {
                    @Override
                    public void onClick(View var1, int selectedIndex, String selectValue, List<Integer> selectIndexs, List<String> selectValues) {
                        if (tv_sex.getText().toString().equals(sexList.get(selectedIndex).toString())){
                            return;
                        }
                        String purpose = sexList.get(selectedIndex).toString();
                        tv_sex.setText(purpose);
                    }
                },sexList,tv_sex.getText().toString(),false,false,true);
            }
        });

        tv_relationtype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.createNewSelectedDialog(RelativeAdd_Activity.this, "亲属关系", new WheelView.OnWheelViewOkClickListener() {
                    @Override
                    public void onClick(View var1, int selectedIndex, String selectValue, List<Integer> selectIndexs, List<String> selectValues) {
                        if (tv_relationtype.getText().toString().equals(relationTypeList.get(selectedIndex).toString())){
                            return;
                        }
                        String purpose = relationTypeList.get(selectedIndex).toString();
                        tv_relationtype.setText(purpose);
                    }
                },relationTypeList,tv_relationtype.getText().toString(),false,false,true);
            }
        });

        tv_education.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.createNewSelectedDialog(RelativeAdd_Activity.this, "学历", new WheelView.OnWheelViewOkClickListener() {
                    @Override
                    public void onClick(View var1, int selectedIndex, String selectValue, List<Integer> selectIndexs, List<String> selectValues) {
                        if (tv_education.getText().toString().equals(educationList.get(selectedIndex).toString())){
                            return;
                        }
                        String purpose = educationList.get(selectedIndex).toString();
                        tv_education.setText(purpose);
                    }
                },educationList,tv_education.getText().toString(),false,false,true);
            }
        });

        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                relativeBean.setName(et_name.getText().toString());
                switch (tv_sex.getText().toString()){
                    case "男":
                        relativeBean.setSex(1);
                        break;
                    case "女":
                        relativeBean.setSex(2);
                        break;
                    default:
                        break;
                }
                switch (tv_relationtype.getText().toString()){
                    case "配偶":
                        relativeBean.setRelationtype(1);
                        break;
                    case "父母":
                        relativeBean.setRelationtype(2);
                        break;
                    case "子女":
                        relativeBean.setRelationtype(3);
                        break;
                    case "兄弟姐妹":
                        relativeBean.setRelationtype(4);
                        break;
                    default:
                        break;
                }
                relativeBean.setCertificatecode(et_certificatecode.getText().toString());
                switch (tv_education.getText().toString()){
                    case "博士研究生":
                        relativeBean.setEducation(1);
                        break;
                    case "硕士研究生":
                        relativeBean.setEducation(2);
                        break;
                    case "大学本科":
                        relativeBean.setEducation(3);
                        break;
                    case "大学专科":
                        relativeBean.setEducation(4);
                        break;
                    case "中专/高中":
                        relativeBean.setEducation(5);
                        break;
                    case "初中 ":
                        relativeBean.setEducation(6);
                        break;
                    case "小学":
                        relativeBean.setEducation(7);
                        break;
                    case "未知":
                        relativeBean.setEducation(8);
                        break;
                    default:
                        break;
                }
                relativeBean.setSpecialty(et_specialty.getText().toString());
                relativeBean.setRemark(et_remark.getText().toString());

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("relative",relativeBean);
                intent.putExtra("relative_bundle",bundle);
                setResult(resultCode,intent);
                finish();
                overridePendingTransitionFade();
            }
        });

    }
}
